<template>
  <el-container style="height: 100%;flex-direction: column;align-items: center;">
    <el-row style="justify-content: center;align-items: center; width: 100%;margin-bottom: 20px;font-size: large;font-weight: bold">
      待检查项目
    </el-row>
    <el-table :data="showItems" table-layout="fixed" stripe style="width: 100%;margin-top: 10px;">
      <el-table-column align="center" prop="departmentId" label="科室" :formatter="formatDepart"/>
      <el-table-column align="center" prop="clinicId" label="门诊" :formatter="formatClinic"/>
      <el-table-column align="center" prop="examItemId" label="检查项目" :formatter="formatExamItem"/>
      <el-table-column align="center" prop="doctorId" label="开单医生" :formatter="formatDoctor"/>
      <el-table-column align="center" label="操作" >
        <template #default="scoped">
          <el-button link type="primary" @click="chooseDoctor(scoped.row)">
            预约
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-container>
</template>

<script setup>
import mitt from '@/utils/mitt'
import { useRouter } from "vue-router";
import {useMainStore} from "@/store";
import {ref} from "vue";
import examRecordApi from "@/apis/examRecord";
import departClinicDoctorItemApi from "@/apis/departClinicDoctorItem";


const emitter = mitt
const router = useRouter();
const store = useMainStore();
const patientInfo = JSON.parse(sessionStorage.getItem('userInfo'))


const next = ()=>{
  // console.log(departAndClinic)
  emitter.emit('next',1);
  router.push({name:'ChooseExamDoctor'});

  // store.setDepartClinicItem(departAndClinic)
}


const recordList = ref([])
const showItems = ref([])

/**
 * 获取所有未预约的检查
 * @returns {Promise<void>}
 */
const getRecordList = async () =>{
  const data = {
    patientId:patientInfo.id
  }
  recordList.value = (await examRecordApi.getUnpaidListByPatientId(data)).data;
  showItems.value = recordList.value;
  // selectedStatus.value = null;
}
getRecordList();

/**
 * 点击 “预约” 进行医生选择
 * @param row
 */
const chooseDoctor = (row) =>{
  // console.log(row) //（ id，患者id、开单医生id、科室id、门诊id）
  const examItemRecordDetails = {
    id:row.id,
    preDoctorId:row.preDoctorId,
    preDoctorName:formatDoctor(row),
    departmentId:row.departmentId,
    departmentName:formatDepart(row),
    clinicId:row.clinicId,
    clinicName:formatClinic(row),
    examItemId:row.examItemId,
    examItemName:formatExamItem(row),
    fee:row.fee
  }
  store.setExamItemRecordDetails(examItemRecordDetails)
  next()
}

/**
 * 获取科室信息、门诊、项目信息
 * @type {Ref<UnwrapRef<*[]>, UnwrapRef<*[]> | *[]>}
 */
const departmentList = ref([])
const clinicList = ref([])
const doctorList = ref([])
const examItemList = ref([])

const getDepartList = async () => {
  const res = await departClinicDoctorItemApi.getDepartList()
  // console.log(res)
  if(res.code === 200){
    departmentList.value = res.data;
  }else {
    departmentList.value = null;
  }
}
getDepartList();

const getClinicList = async () =>{
  const res = await departClinicDoctorItemApi.getClinicList()
  // console.log(res)
  if(res.code === 200){
    clinicList.value = res.data;
  }else {
    clinicList.value = null;
  }
}
getClinicList();

const getDoctorList = async () =>{
  const res = await departClinicDoctorItemApi.getDoctorList();
  // console.log(res)
  if(res.code === 200){
    doctorList.value = res.data;
  }else {
    doctorList.value = null;
  }
}
getDoctorList();

const getExamItemList = async () =>{
  const res = await departClinicDoctorItemApi.getExamItemList()
  if(res.code === 200){
    examItemList.value = res.data;
  }else {
    examItemList.value = null;
  }
}
getExamItemList();

/**
 * table数据格式
 * @param row
 * @returns {*}
 */
function formatDepart(row) {
  const department = departmentList.value.find(item => item.id === row.departmentId);
  // console.log(department);
  return department ? department.name : '未知科室';
}

function formatClinic(row) {
  const clinic = clinicList.value.find(item => item.id === row.clinicId);
  // console.log(clinic);
  return clinic ? clinic.name : '未知门诊';
}

function formatDoctor(row) {
  const doctor = doctorList.value.find(item => item.id === row.preDoctorId);
  // console.log(doctor);
  return doctor ? doctor.name : '未知医生';
}

function formatExamItem(row) {
  const examItem = examItemList.value.find(item => item.id === row.examItemId);
  // console.log(examItem);
  return examItem ? examItem.name : '未知检查项目';
}



</script>



<style scoped>



</style>

